import { mockChatSessions, mockChatMessages, mockUsers, getNewMessageId } from './chatData';
import axios from 'axios';
import MockAdapter from 'axios-mock-adapter';

// 创建 mock 适配器
const mock = new MockAdapter(axios, { delayResponse: 500 }); // 添加延迟以模拟网络请求

export function setupMockApi() {
  // 模拟聊天会话 API
  mock.onGet('/chatsession').reply(200, mockChatSessions);
  
  mock.onGet(/\/chatsession\/\d+/).reply((config) => {
    const sessionId = parseInt(config.url.split('/').pop());
    const session = mockChatSessions.find(s => s.chatSessionID === sessionId);
    return session ? [200, session] : [404, { message: '聊天会话不存在' }];
  });
  
  mock.onGet(/\/chatsession\/user1\/\d+\/user2\/\d+/).reply((config) => {
    const urlParts = config.url.split('/');
    const user1ID = parseInt(urlParts[urlParts.length - 3]);
    const user2ID = parseInt(urlParts[urlParts.length - 1]);
    
    const session = mockChatSessions.find(
      s => (s.user1ID === user1ID && s.user2ID === user2ID) ||
           (s.user1ID === user2ID && s.user2ID === user1ID)
    );
    
    return session ? [200, session] : [404, { message: '聊天会话不存在' }];
  });
  
  mock.onPost('/chatsession').reply((config) => {
    const newSession = JSON.parse(config.data);
    newSession.chatSessionID = mockChatSessions.length + 1;
    mockChatSessions.push(newSession);
    return [201, newSession];
  });
  
  // 模拟聊天消息 API
  mock.onGet('/chatmessage').reply(200, mockChatMessages);
  
  mock.onGet(/\/chatmessage\/\d+/).reply((config) => {
    const messageId = parseInt(config.url.split('/').pop());
    const message = mockChatMessages.find(m => m.messageID === messageId);
    return message ? [200, message] : [404, { message: '消息不存在' }];
  });
  
  mock.onGet(/\/chatmessage\/session\/\d+/).reply((config) => {
    const sessionId = parseInt(config.url.split('/').pop());
    const messages = mockChatMessages.filter(m => m.chatSessionID === sessionId);
    return [200, messages];
  });
  
  mock.onGet(/\/chatmessage\/sender\/\d+/).reply((config) => {
    const senderId = parseInt(config.url.split('/').pop());
    const messages = mockChatMessages.filter(m => m.senderID === senderId);
    return [200, messages];
  });
  
  mock.onPost('/chatmessage').reply((config) => {
    const newMessage = JSON.parse(config.data);
    newMessage.messageID = getNewMessageId();
    mockChatMessages.push(newMessage);
    return [201, newMessage];
  });
  
  mock.onPut(/\/chatmessage\/\d+/).reply((config) => {
    const messageId = parseInt(config.url.split('/').pop());
    const messageData = JSON.parse(config.data);
    
    const index = mockChatMessages.findIndex(m => m.messageID === messageId);
    if (index !== -1) {
      mockChatMessages[index] = { ...mockChatMessages[index], ...messageData };
      return [200, mockChatMessages[index]];
    }
    return [404, { message: '消息不存在' }];
  });
  
  mock.onDelete(/\/chatmessage\/\d+/).reply((config) => {
    const messageId = parseInt(config.url.split('/').pop());
    const index = mockChatMessages.findIndex(m => m.messageID === messageId);
    
    if (index !== -1) {
      const deletedMessage = mockChatMessages[index];
      mockChatMessages.splice(index, 1);
      return [200, deletedMessage];
    }
    return [404, { message: '消息不存在' }];
  });
  
  // 模拟用户 API
  mock.onGet(/\/user\/\d+/).reply((config) => {
    const userId = parseInt(config.url.split('/').pop());
    const user = mockUsers.find(u => u.userID === userId);
    return user ? [200, user] : [404, { message: '用户不存在' }];
  });
  
  console.log('Mock API 已设置');
} 